package com.liao.work.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.liao.work.R;
import com.liao.work.util.SetActionBarTitle;

public class BookActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    private Spinner spinner1;
    private Spinner spinner2;

    private String[] s = new String[]{"A", "B", "C", "D"};
    private String[][] s1 = new String[][]{{"a1", "a2", "a3", "a4"}, {"b1", "b2", "b3", "b4"}, {"c1", "c2", "c3", "c4"}, {"d1", "d2", "d3", "d4"}};
    private ArrayAdapter<String> adapter2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);
        SetActionBarTitle.setTitle(getSupportActionBar(), false, "书籍二级联动");
        initView();
    }

    private void initView() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, s);
        adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, s1[0]);

        spinner1.setAdapter(adapter);
        spinner2.setAdapter(adapter2);
        spinner1.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, s1[position]);
        spinner2.setAdapter(adapter2);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
